import time
from machine import Pin
import utime
 
# 配置DM420S的引脚  
STEP_IN1 = 13  # 输入引脚1  
DIR_IN2 = 12  # 输入引脚2  
EN = 14   # 使能引脚  
  
# 设置引脚为输出模式  
StepPin = Pin(STEP_IN1, Pin.OUT)  
DirPin = Pin(DIR_IN2, Pin.OUT)  
EnPin = Pin(EN, Pin.OUT)  
 
# p2.value(0)
# 
time_value=5
num_cycle=0

StepPin.value(0)
DirPin.value(0)
EnPin.value(0)
#

DirPin.value(0)
for i in range(2000):
    StepPin.value(0)
    utime.sleep_us(100)
    StepPin.value(1)
    utime.sleep_us(500)
    num_cycle +=1
    print(num_cycle)
    

DirPin.value(1)
for i in range(2000):
    StepPin.value(0)
    utime.sleep_us(100)
    StepPin.value(1)
    utime.sleep_us(500)
    num_cycle +=1
    print(num_cycle) 
# 
#   
# # 控制电机正反转  
# def control_motor(direction):  
#     if direction == 'forward':  
#         pin1.value(0)  # 设置IN1为低电平  
#         pin2.value(1)  # 设置IN2为高电平  
#     elif direction == 'backward':  
#         pin1.value(1)  # 设置IN1为高电平  
#         pin2.value(0)  # 设置IN2为低电平  
#     else:  
#         raise ValueError("Invalid direction, must be 'forward' or 'backward'.")  
#   
# # 控制电机启动和停止  
# def control_motor_start_stop(start_stop):  
#     if start_stop == 'start':  
#         pin_en.value(1)  # 使能引脚EN输出高电平，启动电机  
#     elif start_stop == 'stop':  
#         pin_en.value(0)  # 使能引脚EN输出低电平，停止电机  
#     else:  
#         raise ValueError("Invalid start_stop value, must be 'start' or 'stop'.")  
#   
# # 示例程序，控制电机正反转和启动停止  
# try:  
#     while True:  
#         # 控制电机正反转  
#         control_motor('forward')  # 正转5秒  
#         time.sleep(5)  
#         control_motor('backward')  # 反转5秒  
#         time.sleep(5)  
#         # 控制电机启动停止  
#         control_motor_start_stop('start')  # 启动电机5秒  
#         time.sleep(5)  
#         control_motor_start_stop('stop')  # 停止电机5秒  
#         time.sleep(5)  
# except KeyboardInterrupt:  
#     # 捕获异常，停止电机并退出程序  
#     control_motor_start_stop('stop')